<script setup lang="ts" name="SystemMailAccount">
import { storeToRefs } from 'pinia'
import { useAppStoreWithOut } from '@/store/modules/app'
import { DICT_TYPE } from '@/enums/dictEnums'
import { dateTimeFormatter } from '@/utils/dateUtil'
import { MailAccountApi } from '@/api/system/messages/mail/account'
import MailAccountForm from './MailAccountForm.vue'
import MailAccountDetail from './MailAccountDetail.vue'
import { checkPermit, computedOperateWidth } from '@/utils/permit'
const appStore = useAppStoreWithOut()
const { systemConfig } = storeToRefs(appStore)

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化
const total = ref(0) // 列表的总页数
const list = ref([]) // 表格数据
const queryLoading = ref(false) // 列表加载中：1）列表数据加载；2）查询按钮禁用；3）重置按钮禁用
const queryParams = ref({
  pageIndex: 1,
  pageSize: 20,
  username: undefined,
  mail: undefined
})
const queryFormRef = ref() // 搜索的表单


/** 查询列表 */
const getList = async () => {
  queryLoading.value = true
  try {
    const data = await MailAccountApi.page(queryParams.value)
    list.value = data.list
    total.value = data.total
  } finally {
    queryLoading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageIndex = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.value.pageIndex = 1
  queryParams.value.pageSize = 20
  queryParams.value.username = undefined
  queryParams.value.mail = undefined
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 详情操作 */
const detailRef = ref()
const openDetail = (id: number) => {
  detailRef.value.open(id)
}


/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await MailAccountApi.delete(id)
    message.success(t('message.del', 'success'))
    // 刷新列表
    await getList()
  } catch { }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>

<template>
  <PageCard>
    <ContentFormTable>
      <template #header>
        <ElForm ref="queryFormRef" :model="queryParams" :inline="true">
          <ElFormItem :label="t('sys.messages.mail.account', 'mail')" prop="mail" class="!w-240px">
            <el-input v-model="queryParams.mail" :placeholder="t('sys.messages.mail.account.rules', 'mail')" clearable @keyup.enter="handleQuery" />
          </ElFormItem>
          <ElFormItem :label="t('sys.messages.mail.account', 'username')" prop="username" class="!w-240px">
            <el-input v-model="queryParams.username" :placeholder="t('sys.messages.mail.account.rules', 'username')" clearable @keyup.enter="handleQuery" />
          </ElFormItem>
          <ElFormItem>
            <el-button type="primary" :disabled="queryLoading" icon="Search" @click="handleQuery">{{ t('action',
              'search') }}</el-button>
            <el-button icon="Refresh" :disabled="queryLoading" @click="resetQuery">{{ t('action', 'reset')
            }}</el-button>
            <el-button v-if="checkPermit(['system:social-client:create'])" type="success" icon="Plus" @click="openForm('create')">{{ t('action', 'create') }}</el-button>
          </ElFormItem>
        </ElForm>
      </template>
      <template #content>
        <el-table v-loading="queryLoading && systemConfig.loadingType === 'module'" :data="list" border>
          <el-table-column type="index" :label="t('common', 'index')" width="60" align="center" />
          <el-table-column prop="mail" :label="t('sys.messages.mail.account', 'mail')" align="center" />
          <el-table-column :label="t('sys.messages.mail.account', 'username')" align="center" prop="username" />
          <el-table-column :label="t('sys.messages.mail.account', 'host')" align="center" prop="host" />
          <el-table-column :label="t('sys.messages.mail.account', 'port')" align="center" prop="port" />
          <el-table-column align="center" :label="t('sys.messages.mail.account', 'sslEnable')" prop="sslEnable">
            <template #default="scope">
              <dict-tag :type="DICT_TYPE.INFRA_BOOLEAN_STRING" :value="scope.row.sslEnable + ''" />
            </template>
          </el-table-column>
          <el-table-column prop="create_time" :label="t('common', 'createTime')" :formatter="dateTimeFormatter" align="center" />
          <el-table-column prop="operate" :label="t('common', 'operate')" :width="computedOperateWidth(['system:mail-account:update', 'system:mail-account:delete']) + 64" align="center" fixed="right">
            <template #default="scope">
              <el-button v-if="checkPermit(['system:mail-account:update'])" type="primary" icon="Edit" @click="openForm('update', scope.row.id)" />
              <el-button v-if="checkPermit(['system:mail-account:delete'])" type="danger" icon="Delete" @click="handleDelete(scope.row.id)" />
              <el-button type="primary" @click="openDetail(scope.row.id)">{{ t('action', 'detail') }}</el-button>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template #footer>
        <Pagination v-model:limit="queryParams.pageSize" v-model:page="queryParams.pageIndex" :total="total" :disabled="queryLoading" @pagination="getList" />
        <MailAccountForm ref="formRef" @success="getList" />
        <MailAccountDetail ref="detailRef" />
      </template>
    </ContentFormTable>
  </PageCard>
</template>

<style scoped lang="scss">
.el-table {
  width: 100%;
  height: 100%;
}
</style>
